package com.bci.pwtz.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.CompanyAction;
import com.bci.pwtz.mysql.model.CompanyRole;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.CompanyUserRole;
import com.bci.pwtz.service.CompanyRoleService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.CompanyUserRoleService;
@Controller
@RequestMapping("/user/role")
public class CompanyRoleController extends AbstractController
{
    @Autowired
    private CompanyRoleService companyRoleService;
    
    @Autowired
    private CompanyService companyService;
    
    @Autowired
    private CompanyUserRoleService userRoleService;
    //列表
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationList<CompanyRole> list(int pageszie,int pagenum)
    {
    	PaginationList<CompanyRole> list = new PaginationList<CompanyRole>();
        CompanyUser currentUser = (CompanyUser)session.getAttribute("user");
        long currentCompanyID =currentUser.getLastCompanyId();
        try
        {            
            Long[] companyIds = new Long[] {currentCompanyID};
            list = companyRoleService.selectByCompanyIds(companyIds, pagenum, pageszie);
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        return list;
    }
       //添加
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(CompanyRole companyRole){
        Map<String, String> map=new HashMap<String, String>();
        try
        {
        Long companyId=(Long) session.getAttribute("companyId");
        //System.out.println("companyId=" +companyId);
        CompanyRole role=new CompanyRole();
        companyRole.setCompanyRoleId(companyRole.getCompanyRoleId());
        role.setRoleName(companyRole.getRoleName());
        role.setRemark(companyRole.getRemark());
        role.setIsSuper(companyRole.getIsSuper());
        role.setCanDelete(companyRole.getCanDelete());
        role.setCompanyRoleActions(companyRole.getCompanyRoleActions());
        List<CompanyRole> list=companyRoleService.selectRole(companyId, companyRole.getRoleName());
        if(list==null||list.size()==0){
            int ret=companyRoleService.add(companyRole);
            Company company=companyService.load(companyId);
            session.setAttribute("company", company);
            if(ret>0){
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS,ERROR_VALUE_MSG_TYPE_INFO,null,"创建单位角色成功");
            }else{
                map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"创建单位角色失败");
            }
        }else{
            map=packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL,ERROR_VALUE_MSG_TYPE_ERROR,null,"不能创建相同名称的角色");
        }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        return map;
    }
    
 /*   @RequestMapping(value = "/roledel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> roledel(long roleId, long companyId)
    {
        Map<String, String> respMap = new HashMap<String, String>();
        try
        {
            int ret=companyRoleService.deleteCompanyRoleByRoleId(roleId, companyId);
            if (ret ==1 )
            {
                respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"删除成功");
            }
            else
            {
                respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,"ParametersException", "删除失败");
            }

        }
        catch (PwtzDatabaseException e)
        {
            respMap = packageUppException(e);
        }
        catch (Exception e)
        {
            respMap = packageException(e);
        }
        return respMap;
    }
*/

    @RequestMapping(value = "/actionlist", method = RequestMethod.POST)
    @ResponseBody
    public List<CompanyAction> actionlist(long companyRoleId)
    {
    	List<CompanyAction> list = new ArrayList<CompanyAction>();
        try
        {            
            list = companyRoleService.selectCompanyAction();
            for(int i=0;i<list.size();i++){
            	//System.out.println("companyRoleId="+companyRoleId);
            	//System.out.println("getActionCode="+list.get(i).getActionCode());
            	//System.out.println(companyRoleService.checkExistsCompanyRoleAction(companyRoleId, list.get(i).getActionCode()));
            	//System.out.println("list.get(i)="+i);
            	if(companyRoleService.checkExistsCompanyRoleAction(companyRoleId, list.get(i).getActionCode())){
            		list.get(i).setActionType(-1);
            	}
            	//System.out.println(list.get(i).getActionName());
            	//System.out.println(list.get(i).getActionType());
            }
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        return list;
    }
    
    @RequestMapping(value = "/actionadd", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> actionadd(String rolename, long companyRoleId, String jurisdiction)
    {
        Map<String, String> respMap = new HashMap<String, String>();
        try
        {
        	CompanyUser companyUser = (CompanyUser)session.getAttribute("user");
//        	List<CompanyRole> list=companyRoleService.selectRole(companyUser.getLastCompanyId(), rolename);
            int result = companyRoleService.addCompanyRole(rolename, companyUser.getLastCompanyId(), companyRoleId, jurisdiction);
            if (result ==1 )
            {
            	List<CompanyUserRole> ulist=userRoleService.selectUserId(companyRoleId);
            	boolean flag=false;
            	for(CompanyUserRole role:ulist){
            		if(role.getCompanyUserId().equals(companyUser.getCompanyUserId())){
            			flag=true;
            		}
            	}
            	if(flag){
            		List<String> actionList=getCurrentAction();
                    session.setAttribute("actionList", actionList);
                    respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"操作成功");
            	}else{
            		respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"操作成功");
            	}
            }
            else
            {
                respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,"ParametersException", "操作失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            respMap = packageUppException(e);
        }
        catch (Exception e)
        {
            respMap = packageException(e);
        }
        return respMap;
    }
    
    @RequestMapping(value = "/actiondel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> actiondel(long companyRoleId)
    {
        Map<String, String> respMap = new HashMap<String, String>();
        try
        {
        	int result = companyRoleService.delCompanyRole(companyRoleId);
        	if (result == 1 )
            {
                respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"删除成功");
            }else if(result == -1)
            {
            	respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"请先删除该组下面的成员");
            }
            else
            {
                respMap = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,"ParametersException", "删除失败");
            }

        }
        catch (PwtzDatabaseException e)
        {
            respMap = packageUppException(e);
        }
        catch (Exception e)
        {
            respMap = packageException(e);
        }
        return respMap;
    }
}
